package week07.springjdbc;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author edward
 * @since 2021-08-08
 */
@Service
public class ProductService {
//    @Autowired
//    JdbcTemplate jdbcTemplate;
    @Resource
    ProductRepository productRepository;

    @Transactional(timeout = 11)
    public Product addProduct(Product product) {
        return productRepository.save(product);


        /*jdbcTemplate.update("insert into t_product(product_id,product_name,category,price,quantity,create_datetime,update_datetime) " +
                        "values(?,?,?,?,?,?,?)",
                product.getProductId(), product.getProductName(), product.getCategory(),
                product.getPrice(), product.getQuantity(),
                product.getCreateDatetime(), product.getUpdateDatetime());*/
    }

    @Transactional(readOnly = true, timeout = 10)
    public Product getProductById(String id) {
        return productRepository.findById(id).orElse(new Product());


        /*return jdbcTemplate.queryForObject("select * from t_product where product_id=?", (resultSet, rowNum) -> {
            Product product = new Product();
            product.setProductId(resultSet.getString("product_id"));
            product.setProductName(resultSet.getString("product_name"));
            product.setCategory(resultSet.getString("category"));
            product.setPrice(resultSet.getBigDecimal("price"));
            product.setQuantity(resultSet.getInt("quantity"));
            product.setCreateDatetime(resultSet.getDate("create_datetime"));
            product.setUpdateDatetime(resultSet.getDate("update_datetime"));
            return product;
        }, id);*/
    }
}
